package open3366pvp.as3
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.system.Security;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	import open3366pvp.as3.dataStruct.IPvpInfo;
	import open3366pvp.as3.dataStruct.ServerMsg;

	/**
	 * 初始化组件,处理socket关闭、出错、数据派发等
	 * @author mandyxiong
	 * 
	 */	
	public class Open3366PvpCore
	{
		private static var _szUid:String;
		private static var _uiTimeStamp:uint;
		private static var _szAccessToken:String;
		private static var _uiGameId:uint;
		
		private static var _service:*;
		private static var _status:int = 0;
		
		private static var _isSandBox:Boolean;
		
		public static function init(szUid:String, uiTimeStamp:uint, szAccessToken:String, uiGameId:uint, dis:Sprite):void
		{
			_szUid = szUid;
			_uiTimeStamp = uiTimeStamp;
			_szAccessToken = szAccessToken;
			_uiGameId = uiGameId;
			
			Security.allowDomain("www.3366.com");
			Security.allowDomain("3366.com");
			Security.allowDomain("open.3366.com");
			Security.allowDomain("flash.3366.com");
			Security.allowDomain("view.3366.com");
			Security.allowDomain("app.3366.com");
			Security.allowDomain("mgp.qq.com");
			
			_isSandBox = (dis == null ? false : dis.loaderInfo.url.indexOf("img.3366img.com") == -1);
			trace("######################flash环境 url="+dis.loaderInfo.url+"，沙箱环境  _isSandBox="+_isSandBox);
			
			if(_status == 0)
			{
				var swfDomain:String = "www.3366.com";
				loadServerSwf(swfDomain);
			}else if(_status == 2)
			{
				_service.setGame(_szUid, _uiTimeStamp, _szAccessToken, _uiGameId, _isSandBox);
			}
		}
		
		private static function loadServerSwf(swfDomain:String):void
		{
			var swfUrl:String = "http://" + swfDomain + "/open3366/component/openservicepvp_as3.swf?r="+getTimer();
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onServerLoaded);
			loader.load(new URLRequest(swfUrl));
			_status = 1;
		}
		
		private static function onServerLoaded(evt:Event):void
		{
			var loaderInfo:LoaderInfo = evt.target as LoaderInfo;
			loaderInfo.removeEventListener(Event.COMPLETE, onServerLoaded);
			_status = 2;
			_service = loaderInfo.content;
			_service.setEvtCls(Open3366PvpEvent);
			_service.setGame(_szUid, _uiTimeStamp, _szAccessToken, _uiGameId, _isSandBox);
			(_service as EventDispatcher).addEventListener(Open3366PvpEvent.SOCKET_ERROR, onSocketError);
			(_service as EventDispatcher).addEventListener(Open3366PvpEvent.SOCKET_CLOSE, onSocketClose);
			(_service as EventDispatcher).addEventListener(Open3366PvpEvent.SOCKET_DATA, onSocketData);
			Open3366PvpApi.dispatchEvent(new Open3366PvpEvent(Open3366PvpEvent.INIT_GAME_OVER));
			trace("pvp组件加载完成");
		}
		
		private static function onSocketError(evt:Open3366PvpEvent):void
		{
			trace("socket错误。错误描述="+evt.data.toString());
			Open3366PvpApi.dispatchEvent(evt);
		}
		
		private static function onSocketClose(evt:Open3366PvpEvent):void
		{
			trace("socket关闭处理");
			Open3366PvpApi.dispatchEvent(evt);
		}
		
		private static function onSocketData(evt:Open3366PvpEvent):void
		{
			var cmdId:int = evt.data.cmdId;
			var data:ByteArray = evt.data.data;
			var iRet:int = data.readInt();
			var info:IPvpInfo = Open3366PvpConfig.getInfo(cmdId);
			if(info)
			{
				if(iRet == 0)
				{
					info.parasData(data);
				}
				trace("服务器返回数据。cmdId="+cmdId+"iRet="+iRet);
				Open3366PvpApi.dispatchEvent(new Open3366PvpEvent(info.eventType, {info:info, iRet:iRet}));
			}else
			{
				trace("服务器返回数据。cmdId="+cmdId+",data="+data.toString()+",iRet="+iRet);
			}
			if(((cmdId == 325) && (iRet == 0)) || (cmdId == 322 && (info as ServerMsg).msgFlag == 2))
			{
				_service.close();
			}
		}
		
		public static function get service():*
		{
			return _service;
		}
	}
}